<!DOCTYPE HTML>
<html lang="cn" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <%include("/common/header.html",{title:''}){}%>
    <title>商品列表</title>
    <style>
    </style>
</head>
<body>

<div class="x-nav" style="margin: 20px auto auto 15px;">
      <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
        <a href="javascript:;">首页</a><span lay-separator="">/</span>
        <a href="javascript:;">任务管理</a><span lay-separator="">/</span>
        <a><cite>任务列表</cite></a>
      </span>
    <a class="layui-btn layui-btn-primary layui-btn-small" style="margin: -5px 14px 0px 0px;float:right;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh-3"></i>
    </a>
</div>


<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
            <script type="text/html" id="toolbarDemo">
                <button class="layui-btn layuiadmin-btn-useradmin">任务发布信息</button>
            </script>
            <script type="text/html" id="table-useradmin-webuser">
                {{#  if(d.code==null){ }}
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a id="agree" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="agree"><i class="layui-icon layui-icon-edit"></i>同意</a>
                <a id="refuse" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="refuse"><i class="layui-icon layui-icon-edit"></i>拒绝</a>
                {{#  }else if( d.code==0){ }}
                <a style="margin-left: 25px;" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="view"><i class="layui-icon layui-icon-edit"></i>查看</a>
                <a id="agreed" class="layui-btn layui-btn-normal layui-btn-xs" style="background-color: gray;margin-left: 10px;" lay-event=""><i class="layui-icon layui-icon-edit"></i>已同意</a>
                {{#  }else if( d.code==1){ }}
                <a style="margin-left: 12px;" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="reason1"><i class="layui-icon layui-icon-edit"></i>拒绝原因</a>
                <a id="refused" class="layui-btn layui-btn-normal layui-btn-xs" style="background-color: gray;margin-left: 10px;" lay-event=""><i class="layui-icon layui-icon-edit"></i>已拒绝</a>
                {{#  } }}
            </script>
        </div>
    </div>
</div>
<!-- 弹出页面 -->
<div class="layui-form" lay-filter="layuiadmin-form-useradmin"
     id="layuiadmin-form-useradmin"
     style="padding: 15px;display: none">
    <form action class="layui-form" lay-filter="component-form-element" id="myform">
        <input type="hidden" name="id">
        <input type="hidden" name="u_id">
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">任务标题:</label>
            <div class="layui-input-inline">
                <input type="text" name="title" lay-verify="required" value="" class="layui-input"
                       placeholder="请输入任务标题">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">任务内容:</label>
            <div class="layui-input-inline">
                <textarea type="text" name="content" lay-verify="required" value="" class="layui-input"
                          placeholder="请输入任务内容"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">所需人数:</label>
            <div class="layui-input-inline">
                <input type="text" name="need" lay-verify="required" value="" class="layui-input"
                       placeholder="请输入所需人数">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">补贴费用:</label>
            <div class="layui-input-inline">
                <input type="text" name="integral" lay-verify="required" value="" class="layui-input"
                       placeholder="请输入补贴费用">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">任务时间:</label>
            <div class="layui-input-inline">
                <input type="text" name="t_time" id="t_time" lay-verify="required" value="" class="layui-input"
                       placeholder="请输入任务时间">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block" style="margin-left: 240px;">
                <button class="layui-btn" lay-submit="" lay-filter="component-form-element">立即提交</button>
            </div>
        </div>
    </form>
</div>

<!-- 弹出查看页面 -->
<div class="layui-form" lay-filter="layuiadmin-form-useradmin"
     id="layuiadmin-form-useradmin1"
     style="padding: 15px;display: none">
    <form action class="layui-form" lay-filter="component-form-element1" id="myform1">
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">任务标题:</label>
            <div class="layui-input-inline">
                <input readonly type="text" name="title" lay-verify="required" value="" class="layui-input"
                       placeholder="请输入任务标题">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">任务内容:</label>
            <div class="layui-input-inline">
                <textarea readonly type="text" name="content" lay-verify="required" value="" class="layui-input"
                          placeholder="请输入任务内容"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">所需人数:</label>
            <div class="layui-input-inline">
                <input readonly type="text" name="need" lay-verify="required" value="" class="layui-input"
                       placeholder="请输入所需人数">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">补贴费用:</label>
            <div class="layui-input-inline">
                <input readonly type="text" name="integral" lay-verify="required" value="" class="layui-input"
                       placeholder="请输入补贴费用">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">任务时间:</label>
            <div class="layui-input-inline">
                <input readonly type="text" name="t_time" lay-verify="required" value="" class="layui-input"
                       placeholder="请输入任务时间">
            </div>
        </div>

    </form>
</div>
<%include("/common/foot.html"){}%>
<script>
    layui.use(['index', 'table', 'form', 'laydate', 'layedit', 'AjaxCommon', 'AliangLayui'], function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var AjaxCommon = layui.AjaxCommon;
        var AliangLayui = layui.AliangLayui;

        var title = "修改商品服务";
        var findAll_url = 'system/task/findAll.json';
        var update_url = 'system/task/update.json';
        var agree_url='system/task/agree.json';
        var refuse_url='system/task/refuse.json';
        var index;//定义建立富文本编辑器全局变量
        var layedit//定义富文本编辑器layedit全局变量
        var cols = [ //表头
            {field: 'title', title: '任务标题',},
            {field: 'content', title: '任务内容',},
            {field: 'need', title: '所需人数',},
            {field: 't_time',templet:"<div>{{layui.util.toDateString(d.t_time, 'yyyy-MM-dd')}}</div>", title: '任务时间',},
            {field: 'integral', title: '补贴费用',},
            {field: 'time',  templet:"<div>{{layui.util.toDateString(d.time, 'yyyy-MM-dd HH:mm:ss')}}</div>",title: '发布时间'},
            {title: '操作', fixed: 'right', width: 215, toolbar: '#table-useradmin-webuser',}
        ];
        AliangLayui.createTable(
            "#LAY-user-manage",
            "listReload",
            "#toolbarDemo",
            title,
            findAll_url,
            true,
            [10, 15, 20, 25],
            ['filter', 'print', 'exports'],
            cols,
            null
        );

        //初始化日期模块
        layui.use('laydate', function () {
            var laydate = layui.laydate;
            //初始化查询的开始时间
            laydate.render({
                elem: '#t_time' //指定元素
            });
        });

        //初始化修改页面

        function loadData(data) {

            var date = new Date(data.t_time);
            var year = date.getFullYear(data.t_time);
            var month = date.getMonth(data.t_time) + 1;
            var day = date.getDate(data.t_time);
            month = month < 10 ? "0" + month : month;
            day = day < 10 ? "0" + day : day;
            date = year + '-' + month + '-' + day;

            var ret = {
                "id": data.id,
                "title": data.title,
                "content": data.content,
                "time": data.time,
                "u_id":data.u_id,
                "need":data.need,
                "integral":data.integral,
                "t_time":date,
            }
            form.render();
            return ret;
        }

        laydate.render({
            elem: '#createDate' //指定元素
        });
        //////////////////////////////////////以上是修改每个页面修改的地方
        //弹出修改页面调用初始化数据方法
        function initUpdate(data) {
            var a = layer.open({
                type: 1,
                title: title,
                content: $('#layuiadmin-form-useradmin'),
                btn: ['关闭'], //可以无限个按钮
                cancel: function (index, layero) {//取消事件
                    location.replace(location.href);　//layer.open关闭刷新
                },
                success: function (layero, index) {
                    layer.iframeAuto(index);
                }
            });
            layer.full(a);
            //如果是添加则重置表单，如果是编辑则给表单赋值
            if (data == 0 || data == null) {
                $('#myform')[0].reset();
                return;
            } else {
                layui.form.val("component-form-element", loadData(data));
            }
        }

        //弹出查看页面调用初始化数据方法
        function initUpdate1(data) {
            var a = layer.open({
                type: 1,
                title: title,
                content: $('#layuiadmin-form-useradmin1'),
                btn: ['关闭'], //可以无限个按钮
                cancel: function (index, layero) {//取消事件
                    //location.replace(location.href);　//layer.open关闭刷新
                },
                success: function (layero, index) {
                    layer.iframeAuto(index);
                }
            });
            layer.full(a);
            //给表单赋值

            layui.form.val("component-form-element1", loadData(data));

        }
        //修改页面的内容提交到后台处理
        form.on('submit(component-form-element)', function (data) {
            postUpdate(data);
            return false;
        });
        //头工具栏事件
        table.on('toolbar(LAY-user-manage)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    initUpdate(0);
                    break;
                case 'delete':
                    var data = checkStatus.data;
                    if (data.length > 0) {
                        //var id = new Array(); //声明数组
                        for (var i in data) {
                            var a = data[i].id; //得到复选框的值
                            postDelete(a);
                        }
                    } else {
                        layer.msg("至少选择一个进行删除");
                    }
                    break;
                case 'export':
                    layer.msg("导出");
                    break;
            }
        });

        //点击每行右工具栏“操作”里面按钮效果
        table.on('tool(LAY-user-manage)', function(obj) {
            var data = obj.data;
            if (obj.event === 'agree') {
                $.ajax({
                    url : agree_url,
                    type : "POST",
                    data : {
                        "id" : data.id,
                        "code" : 0,
                    },
                    dataType : "json",
                    success : function(ret) {
                        //alert(ret.code)
                        layer.msg(ret.msg, {icon: 6});

                        setTimeout(function (){

                            window.location.reload();
                        }, 1000);

                    },
                    error:function(jqXHR, textStatus, errorThrown){
                        var estr="Page:"+window.location.pathname+",Function: postDelete()";
                        layer.open({
                            title: "错误信息",
                            area: ['600px', '600px'],
                            type: 2,
                            content: "base/error.jhtml?info="+estr, //
                            btn: ['关闭'] //可以无限个按钮
                        });
                    }
                });
            } else if(obj.event === 'refuse'){
                layer.prompt({title: '请输入驳回原因', formType: 2}, function(text, index){
                    layer.close(index);
                    $.ajax({
                        url : refuse_url,
                        type : "POST",
                        data : {
                            "id" : data.id,
                            "code" : 1,
                            "reason1" : text
                        },
                        dataType : "json",
                        success : function(ret) {
                            layer.msg(ret.msg, {icon: 6});
                            setTimeout(function (){
                                window.location.reload();
                            }, 1000);
                        },
                        error:function(jqXHR, textStatus, errorThrown){
                            var estr="Page:"+window.location.pathname+",Function: postDelete()";
                            layer.open({
                                title: "错误信息",
                                area: ['600px', '600px'],
                                type: 2,
                                content: "base/error.jhtml?info="+estr, //
                                btn: ['关闭'] //可以无限个按钮
                            });
                        }
                    });
                });
            }else if (obj.event === 'reason1') {
                layer.alert(data.reason1)
            }else if (obj.event === 'edit') {
                initUpdate(data);
            }else if (obj.event === 'view') {
                initUpdate1(data);
            }
        });



        //ajax提交删除内容
        window.postDelete = function (id) {
            AjaxCommon.ajax(delete_url, "post", "json", {"id": id}, function (ret) {
                if (ret == null) {
                    layer.msg("删除失败", {
                        icon: 5
                    });
                } else {
                    layer.msg("删除成功", {
                        icon: 6
                    });
                    layer.closeAll();
                    table.reload('listReload');
                }
                return ret;
            });
        };

        //ajax提交修改内容
        window.postUpdate = function (data) {
            var formData = new FormData();
            formData.append("data", JSON.stringify(data.field));
            $.ajax({
                url: update_url,
                type: "POST",
                //traditional:true,
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data.code == 0) {
                        layer.closeAll();
                        layer.msg(data.msg, {time: 600, icon: 1}, function () {
                            //table.reload('listReload');
                            location.replace(location.href);
                        });
                    } else {
                        layer.msg(data.msg, {icon: 5});
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var estr = "Page:" + window.location.pathname + ",Function:postUpdate()";
                    layer.open({
                        title: "错误信息",
                        type: 2,
                        content: "base/error.jhtml?info=" + estr,
                        btn: ['关闭'] //可以无限个按钮
                    });
                }
            });
        }
    });

</script>
</body>

</html>
